<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

<script type="text/javascript">
    var rowId = 0;
    
    $('.ui-reset , #refresh_employeeGrid').click(function(){
        $("#employeeGrid").jqGrid('setGridParam',{search:false});
        var postData = $("#employeeGrid").jqGrid('getGridParam','postData');
        $.extend(postData,{searchField:"",searchString:"",searchOper:""});
        $.extend(postData,{filters:""});
        $("#employeeGrid").trigger("reloadGrid");
    });

    $.subscribe('employeeGridRowSelected', function(event,data) {
        rowId = event.originalEvent.id;
        $( "#employeeGrid_edit" ).button( "option", "disabled", false );
        $( "#employeeGrid_delete" ).button( "option", "disabled", false );
    });

    $.subscribe('searchEmployeeGrid', function(event,data) {
        $("#employeeGrid").jqGrid('searchGrid', {drag:true,closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:"Search Employee"} );
    });

    $.subscribe('addEmployeeGrid', function(event,data) {
        $("#employeeGrid").jqGrid('editGridRow',"new",{drag:true,closeAfterEdit:false,closeOnEscape:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,position:"last",afterSubmit:function(response, postdata) {return isError(response.responseText);}});
    });

    $.subscribe('editEmployeeGrid', function(event,data) {
        if(rowId!=0){
            $("#employeeGrid").jqGrid('editGridRow',rowId,{drag:true,closeAfterEdit:true,closeOnEscape:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select an employee first.");
        }
    });

    $.subscribe('deleteEmployeeGrid',function(event,data){
        if(rowId!=0){
            $("#employeeGrid").jqGrid('delGridRow',rowId,{drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,left:310,top:20,reloadAfterSubmit:true});
        }else{
            alert("Sorry. Please select an employee first.");
        }
    });
    
    $.subscribe('employeeGridComplete',function(event,data){
        $( "#employeeGrid_search" ).button( "option", "disabled", false );
        $( "#employeeGrid_add" ).button( "option", "disabled", false );
    });
    
    function isError(text) {
        if(text.indexOf('ERROR') >= 0) { return [false, text]; }
        return [true,'Done.'];
    }

    function lengthCheck(value, colname) {
        if (value.length < 6 || value.length >11)
            return [false," Id Number must be between 6 to 11 digits only."];
        else
            return [true,""];
    }
</script>
<div class="gridOptsContainer">
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="employeeGrid_search" onClickTopics="searchEmployeeGrid" value="Search Employee"/>
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="employeeGrid_add" onClickTopics="addEmployeeGrid" value="Add Employee"/>
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="employeeGrid_edit" onClickTopics="editEmployeeGrid" value="Edit Employee"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="employeeGrid_delete" onClickTopics="deleteEmployeeGrid" value="Delete Employee"/>
</div>
<s:url id="remoteurl" action="grid-employee"/>
<s:url id="editurl" action="edit-employee"/>
<s:url id="selectOptionsUsertypes" action="select-usertypes"/>
<s:url id="selectOptionsBranches" action="select-branches"/>
<sjg:grid
    id="employeeGrid"
    caption="Cashmate Employees"
    dataType="json"
    href="%{remoteurl}"
    editurl="%{editurl}"
    gridModel="gridModel"
    groupField="['branchname']"
    groupColumnShow="[true]"
    groupCollapse="false"
    groupText="['<b>{0} - {1} Employee(s)</b>']"
    pager="true"
    pagerButtons="true"
    pagerInput="true"
    rowList="10,20,50"
    rowNum="10"
    loadingText="Requesting Data..."
    width="970"
    shrinkToFit="true"
    hoverrows="true"
    rownumbers="true"
    autoencode="true"
    viewrecords="true"
    navigator="true"
    navigatorAdd="false"
    navigatorDelete="false"
    navigatorEdit="false"
    navigatorSearch="false"
    navigatorView="true"
    onSelectRowTopics="employeeGridRowSelected"
    onGridCompleteTopics="employeeGridComplete"
    >

    <sjg:gridColumn name="employeeid"
                    key="true"
                    index="employeeid"
                    title="Employee No. (Not Editable)"
                    sortable="false"
                    hidden="true"
                    align="center"
                    />

    <sjg:gridColumn name="idnumber"
                    index="idnumber"
                    title="I.D. Number"
                    sortable="true"
                    editable="true"
                    edittype="text"
                    search="true"
                    editrules="{number:true,required:true,custom:true,custom_func:lengthCheck}"
                    searchoptions="{sopt:['eq']}"
                    editoptions="{maxlength:'11'}"
                    align="center"
                    />

    <sjg:gridColumn name="firstname"
                    index="firstname"
                    title="First Name"
                    sortable="true"
                    editoptions="{maxlength:'45'}"
                    editrules="{required:true}"
                    editable="true"
                    edittype="text"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    align="center"
                    />

    <sjg:gridColumn name="middlename"
                    index="middlename"
                    title="Middle Name"
                    search="false"
                    editable="true"
                    edittype="text"
                    editoptions="{maxlength:'45'}"
                    editrules="{required:true}"
                    align="center"
                    />

    <sjg:gridColumn name="lastname"
                    index="lastname"
                    title="Last Name"
                    sortable="true"
                    editrules="{required:true}"
                    editable="true"
                    editoptions="{maxlength:'45'}"
                    edittype="text"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    align="center"
                    />

    <sjg:gridColumn name="type"
                    index="type"
                    title="Role"
                    sortable="true"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    editable="true"
                    edittype="select"
                    editoptions="{dataUrl : '%{selectOptionsUsertypes}'}"
                    align="center"
                    />

    <sjg:gridColumn name="branchname"
                    index="branchname"
                    title="Branch"
                    sortable="false"
                    search="true"
                    editable="true"
                    searchoptions="{sopt:['eq']}"
                    edittype="select"
                    editoptions="{dataUrl : '%{selectOptionsBranches}'}"
                    align="center"
                    />
</sjg:grid>
